package com.kaili.basic.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.kaili.basic.mapper.BasicSiteMapper;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.BaseCityVO;
import com.kaili.basic.pojo.entity.BasicSite;
import com.kaili.basic.pojo.vo.BaseCarVO;
import com.kaili.basic.service.BaseCityService;
import com.kaili.basic.service.BasicSiteService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangjl
 * @Type BasicSiteController
 * @Desc 杭州凯立通信有限公司
 * @Date 2018年04月26日
 */

@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Validated
@Api(tags = "网点管理", description = "网点管理相关API")
public class BasicSiteController {

    @Autowired
    private BasicSiteService basicSiteService;

    @Autowired
    private BaseCityService baseCityService;
    @Autowired
    private BasicSiteMapper basicSiteMapper;

    /**
     * 新增网点
     *
     * @param baseSiteAddDto
     * @return
     */
    @ApiOperation(value = "新增网点", notes = "新增网点")
    @PostMapping(value = "baseSite")
    public RestStatus addBasicSite(@Validated @RequestBody BaseSiteAddDto baseSiteAddDto) {
        BasicSite basicSite = new BasicSite();
        BeanUtils.copyProperties(baseSiteAddDto, basicSite);
        BasicSite basicSite1 = basicSiteMapper.loadBySite(basicSite);
        if (basicSite1 != null){
            return CommonStatus.SITE_CODE_EXIST;
        }
        boolean flag = basicSiteService.addBasicSite(basicSite);
        if (flag) {
            return CommonStatus.ADD_OK;
        } else {
            return CommonStatus.ADD_FAIL;
        }
    }

    /**
     * 修改网点
     *
     * @param baseSiteUpdateDto
     * @return
     */
    @ApiOperation(value = "修改网点", notes = "修改网点")
    @PutMapping(value = "baseSite")
    public RestStatus updateBasicSite(@Validated @RequestBody BaseSiteUpdateDto baseSiteUpdateDto) {
        BasicSite basicSite = new BasicSite();
        BeanUtils.copyProperties(baseSiteUpdateDto, basicSite);
        int flag = basicSiteService.updateBasicSite(basicSite);
        //查找到记录，并修改成功
        if (flag > 0) {
            return CommonStatus.UPDATE_OK;
        } else {
            return CommonStatus.UPDATE_FAIL;
        }
    }

    /**
     * 删除网点
     *
     * @param siteId
     * @return
     */
    @ApiOperation(value = "删除网点", notes = "删除网点")
    @DeleteMapping(value = "baseSite/{siteId}")
    public RestStatus delete(@PathVariable Long siteId) {
        BasicSite basicSite1 = basicSiteService.getBySiteId(siteId);
            if (basicSite1 != null) {
                basicSite1.setRdStatus(0);
                basicSiteService.updateBasicSite(basicSite1);
            }
        return CommonStatus.DELETE_OK;
    }

    /**
     * 分页条件查询列表
     * @param baseSiteQueryDto
     * @return
     */
    @ApiOperation(value = "分页条件查询列表", notes = "分页条件查询列表")
    @GetMapping(value = "baseSite/page")
    public Page<BasicSite> listBasicSite(@ModelAttribute BaseSiteQueryDto baseSiteQueryDto) {
        BasicSite basicSite = new BasicSite();
        basicSite.setSiteName(baseSiteQueryDto.getSiteName());
        basicSite.setCityName(baseSiteQueryDto.getCityName());
        basicSite.setContacts(baseSiteQueryDto.getContacts());
        basicSite.setSiteCode(baseSiteQueryDto.getSiteCode());
        basicSite.setPage(baseSiteQueryDto.getPage());
        basicSite.setPageSize(baseSiteQueryDto.getPageSize());
        return basicSiteService.listBasicSite(new Page<BasicSite>(basicSite.getPage(), basicSite.getPageSize()), basicSite);
    }

    /**
     * 网点名称或编号模糊查询
     * @param
     * @return
     */
    @ApiOperation(value = "网点名称或编号模糊查询", notes = "网点名称或编号模糊查询")
    @GetMapping("baseSite/list")
    public List<BasicSite> querySiteNameCode(@ModelAttribute BaseSiteLikeDto baseSiteLikeDto) {
        return basicSiteService.querySiteNameCode(baseSiteLikeDto.getSiteName());
    }

    /**
     * 根据网点ID查询
     * @param baseSiteByIdDto
     * @return
     */
    @ApiOperation(value = "根据网点ID查询", notes = "根据网点ID查询")
    @GetMapping(value = "baseSite/id")
    public BasicSite getBySiteId(@ModelAttribute BaseSiteByIdDto baseSiteByIdDto){
        BasicSite basicSite = basicSiteService.getBySiteId(baseSiteByIdDto.getSiteId());
        return basicSite;
    }

    /**
     * 模糊区县查询省市
     * @param baseCityCountyNameDto
     * @return
     */
    @ApiOperation(value = "模糊区县查询省市", notes = "模糊区县查询省市")
    @GetMapping("baseCite/countyName")
    public List<BaseCityVO> getCountyName(@ModelAttribute BaseCityCountyNameDto baseCityCountyNameDto) {
        BaseCityVO baseCityVO = new BaseCityVO();
        baseCityVO.setCountyName(baseCityCountyNameDto.getCountyName());
        return basicSiteService.getCountyName(baseCityVO);
    }

    /**
     * 模糊查询省市区县(向上查询flag 1)
     * @param baseCityLikeDto
     * @return
     */
    @ApiOperation(value = "模糊查询省市区县(向上查询flag 1)", notes = "模糊查询省市区县(向上查询flag 1)")
    @GetMapping("baseCite/like")
    public List<BaseCityVO> getAreaByName(@ModelAttribute BaseCityLikeDto baseCityLikeDto ) {
        BaseCityVO baseCityVO = new BaseCityVO();
        baseCityVO.setCountyName(baseCityLikeDto.getCountyName());
        baseCityVO.setCityName(baseCityLikeDto.getCityName());
        baseCityVO.setProvinceName(baseCityLikeDto.getProvinceName());
        if(StringUtils.isEmpty(baseCityVO.getCountyName())) {
            if(StringUtils.isEmpty(baseCityVO.getCityName()) ) {
                return baseCityService.getProvinceName(baseCityVO);
            }else {
                return baseCityService.getCityName(baseCityVO);
            }
        }
        return baseCityService.getAreaByName(baseCityVO);
    }

    @ApiOperation(value = "获取网点地址和城市名", notes = "获取网点地址和城市名")
    @GetMapping ("baseSite/getSiteAddressAndCity")
    public BasicSite getSiteAddressAndCity(){
        BasicSite basicSite = basicSiteService.getSiteAddressAndCity(LoginDetailUtils.getSiteId());
        return basicSite;
    }

    @ApiOperation(value = "获取区县", notes = "获取区县")
    @GetMapping("baseSite/county")
    public List<Map> getCounty() {
        return baseCityService.getCounty();
    }

    @ApiOperation(value = "获取城市", notes = "获取城市")
    @GetMapping("baseSite/city")
    public List<Map> getCity() {
        return baseCityService.getCity();
    }

    @ApiOperation(value = "获取省份", notes = "获取省份")
    @GetMapping("baseSite/province")
    public List<Map> getProvince() {
        return baseCityService.getProvince();
    }

    @ApiOperation(value = "获取地址", notes = "获取地址")
    @GetMapping("baseSite/area")
    public List<Map> getArea() {
        return baseCityService.getArea();
    }

    @ApiOperation(value = "根据名字获取网点", notes = "根据名字获取网点")
    @RequestMapping("/baseSiteName/{name}")
    public String querySiteName(@RequestParam("name") String name) {
        BasicSite basicSite = basicSiteService.querySiteName(name);
        return basicSite.getSiteId().toString();
    }

    @ApiOperation(value = "根据名字获取城市", notes = "根据名字获取城市")
    @RequestMapping("/baseCityName/{name}")
    public String queryCityName(@RequestParam("name") String name) {
        BaseCityVO cityVO = baseCityService.selectCityByName(name);
        return cityVO.getCityId().toString();
    }
}
